'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
//登录接口
  async login(){
    const {ctx}=this;
    // const post = await this.app.mysql.get('posts', { id: 12 });
    let {user,pas}=ctx.request.body;
    const data=await this.app.mysql.get('userlist',{user,pas})
    if(data){
          ctx.body={
            code:200,
            msg:'登陆成功',
          }
    }else{
      ctx.body={
        code:400,
        msg:'登录失败'
      }
    }
  }

  //注册接口
  async sign(){
    const {ctx}=this;
    const {user,pas}=ctx.request.body;
    const data=await this.app.mysql.get('userlist',{user,pas})
    if(data){
        ctx.body={
          code:400,
          msg:'用户名已存在',
        }
    }else{
      // const result = await this.app.mysql.insert('posts', { title: 'Hello World' }); // 在 post 表中，插入 title 为 Hello W
      const newData=await this.app.mysql.insert('userlist',{
        user:`${user}`,
        pas:`${pas}`
      })
        ctx.body={
          code:200,
          msg:'注册成功，请登录',
          data:newData
        }
    } 
  }
}

module.exports = HomeController;
